package com.matchmaking.system.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.matchmaking.system.config.MapConverter;
import com.matchmaking.system.mapper.SysDeptMapper;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;

import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.matchmaking.system.domain.SysDept;
import com.matchmaking.system.service.SysDeptService;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

@Service
public class SysDeptServiceImpl implements SysDeptService {
    @Autowired
    private SysDeptMapper sysDeptMapper;


    @Override
    public void saveDept(SysDept sysDept) {
        sysDeptMapper.saveDept(sysDept);
    }

    @Override
    public List<SysDept> listAll(SysDept sysDept) {
       List<SysDept> listAll = sysDeptMapper.listAll(sysDept);
        return listAll;
    }

    @Override
    public void updateDept(SysDept sysDept) {
        sysDeptMapper.updateDept(sysDept);
    }

    @Override
    public SysDept getById(Integer id) {
       SysDept sysDept = sysDeptMapper.getById(id);
       return sysDept;
    }

    @Override
    public void removeId(SysDept sysdept) {
        sysDeptMapper.removeId(sysdept);
    }

    @Override
    public SysDept updateSysDept(Long id) {
        SysDept sysDept = sysDeptMapper.updateSysDept(Long.valueOf(id));
        return sysDept;
    }

    @Override
    public List<SysDept> listCount() {
        List<SysDept> list = sysDeptMapper.listCount();
        return list;
    }

    @Override
    public List<SysDept> download() {
        List<SysDept> list = sysDeptMapper.download();
        return list;
    }

    @Override
    public List<SysDept> listCountB() {
        List<SysDept> list = sysDeptMapper.listCountB();
        return list;
    }

    @Override
    public List<SysDept> listCountC() {
        List<SysDept> list = sysDeptMapper.listCountC();
        return list;
    }

    @Override
    public List<SysDept> listCountD() {
        List<SysDept> list = sysDeptMapper.listCountD();
        return list;
    }

    @Override
    public List<SysDept> selectExport(SysDept sysDept) {
        List<SysDept> listAll = sysDeptMapper.selectExport(sysDept);
        return listAll;
    }

    @Override
    public void exprot(List<SysDept> list, HttpServletResponse response) {
       try {

           response.setContentType("application/vnd.ms-excel");
           response.setCharacterEncoding("utf-8");
           String fileName = URLEncoder.encode("部门数据", "UTF-8");
           response.setHeader("Content-disposition","attachment;filename=" + fileName + ".xlsx");

           //这里需要设置不关闭流
           WriteCellStyle headWriteCellStyle = new WriteCellStyle();
           //设置背景颜色
           headWriteCellStyle.setFillBackgroundColor(IndexedColors.WHITE.getIndex());
           //设置头字体
           WriteFont headWriteFont = new WriteFont();
           headWriteFont.setFontHeightInPoints((short)13);
           headWriteFont.setBold(true);
           headWriteCellStyle.setWriteFont(headWriteFont);
           //设置头居中
           headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
           //内容策略
           WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
           //设置水平居中
           contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
           //内容自动换行
           contentWriteCellStyle.setWrapped(true);
           //设置文本收缩至合适
           contentWriteCellStyle.setShrinkToFit(true);
           //垂直居中
           contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
           //设置左边框
           contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
           //设置有边框
           contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
           //设置上边框
           contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
           //设置下边框
           contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);

           HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                   new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);

           EasyExcel.write(response.getOutputStream(),SysDept.class)
                   .registerWriteHandler(horizontalCellStyleStrategy)
                   .registerConverter(new MapConverter())
                   //导出文件名
                   .autoCloseStream(Boolean.TRUE)
                   .sheet()
                   .doWrite(list);


       } catch (Exception e){
            e.printStackTrace();
       }

    }


}
